/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelBiseccion.java
 *
 * Created on 11-oct-2010, 14:12:47
 */
package interfaz.FuncionPanels;

//import java.awt.FlowLayout;
//import javax.print.DocFlavor.STRING;
//import javax.management.monitor.GaugeMonitor;
import interfaz.NuMethodApp;
import interfaz.PanelMain;
import java.awt.Color;
import javax.swing.JFrame;
//import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import metodos.inpol.InterpolacionNewton;




/**
 *
 * @author The NuMethod Team
 */
public class PanelInterNewton extends javax.swing.JPanel {
    private static DefaultTableModel ma;
    private static NuMethodApp parent;

    public void setlinksColor (Color c){
        jXHyperlink1.setClickedColor(c);
        jXHyperlink1.setUnclickedColor(c);

    }
    public PanelInterNewton(NuMethodApp parent) {
        ma = new DefaultTableModel();
        initComponents();
        initComponents2();
        this.parent = parent;

    }





    // <editor-fold defaultstate="collapsed" desc="Helpful getters">
        public static DefaultTableModel getMa() {
        return ma;
    }

    // </editor-fold>
    private void initComponents2() {
        TablePuntos.setShowGrid(true);
        TablePuntos.setOpaque(true);
        TablePuntos.setAutoStartEditOnKeyStroke(true);
        TablePuntos.setRolloverEnabled(true);
        TablePuntos.setSurrendersFocusOnKeystroke(true);
        TablePuntos.setTerminateEditOnFocusLost(true);
        TablePuntos.setFillsViewportHeight(true);


        TablePuntos.setVisibleColumnCount(0);

        rebuildTableModels(3);

        LabelSalida1.setText("");
        LabelSalida2.setText("");

    }

     public void updateTableModels (){
            DefaultTableModel m = PanelInterpolacion.getMa();
            TablePuntos.setModel(m);
            m.fireTableDataChanged();
    }

    /**
     * Metodo que reconstruye de forma dinamica los modelos de tabla
     * luego de cada entrada de datos
     * @param a el numero de ecuaciones del sistema
     */
    private void rebuildTableModels (Integer a){
         //Constuir el modelo para la tabla A
            Double[][] test = new Double[2][a];
            String[] names = new String[a];

            for (int i = 0; i < a; i++) {
                String aTest = "Punto (x" + (i) + "," + "y" + (i)+")";
                names[i] = aTest;
            }

            ma = new DefaultTableModel(test, names);
            TablePuntos.setModel(ma);

            ma.fireTableDataChanged();
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        TablePuntos = new org.jdesktop.swingx.JXTable();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        TableResult = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jXHyperlink1 = new org.jdesktop.swingx.JXHyperlink();
        LabelSalida1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        LabelSalida2 = new javax.swing.JLabel();

        setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        setToolTipText("Interpolacion de Newton");

        TablePuntos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        TablePuntos.setEditable(false);
        jScrollPane1.setViewportView(TablePuntos);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/help.png"))); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel4.setText("Matriz A");

        jLabel1.setText("Polinomio interpolante de Newton");

        jButton1.setText("Ejecutar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        TableResult.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane4.setViewportView(TableResult);

        jLabel2.setText("Cuidado: Los puntos solo pueden ser cambiados desde el panel de entrada");

        jXHyperlink1.setText("Ir al panel de entrada");
        jXHyperlink1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jXHyperlink1ActionPerformed(evt);
            }
        });

        LabelSalida1.setText("jLabel3");

        jScrollPane2.setBorder(null);

        LabelSalida2.setText("jLabel5");
        jScrollPane2.setViewportView(LabelSalida2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(jLabel1)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 416, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 41, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 627, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 627, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(269, Short.MAX_VALUE)
                        .addComponent(jLabel2))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(522, Short.MAX_VALUE)
                        .addComponent(jXHyperlink1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 617, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(LabelSalida1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                        .addComponent(jLabel4)
                        .addGap(2, 2, 2))
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jXHyperlink1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(174, 174, 174)
                .addComponent(LabelSalida1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        NuMethodApp.helpLauncher("Newton-Diferenciasdivididas.html");
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * Metodo que extrae de los textfield`s los valores de los parametros
     * para los metodos iterativos.
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        double[] x = new double[TablePuntos.getModel().getColumnCount()];
        double[] fx = new double[TablePuntos.getModel().getColumnCount()];

        for (int i = 0 ; i < x.length ; i++){
            x[i] = Double.parseDouble((String)TablePuntos.getModel().getValueAt(0, i));
            fx[i] = Double.parseDouble((String)TablePuntos.getModel().getValueAt(1, i));
        }

        double[] result = InterpolacionNewton.solucionar(x, fx);


        Double [][] newModel = new Double[1][result.length];
        for (int i = 0 ; i < result.length ; i++){
             newModel[0][i] = result[i];
        }
        String[] names = new String[result.length];
        for (int i = 0 ; i < names.length ; i++){
             String help = "B" + String.valueOf(i);
             names[i] = help;
        }

        DefaultTableModel dt = new DefaultTableModel(newModel, names);

        TableResult.setModel(dt);
        dt.fireTableDataChanged();

        StringBuilder s = new StringBuilder();

        LabelSalida1.setText("El polinomio interpolante de newton que interpola los puntos dados es:");;
        s.append("p(x)= ").append(newModel[0][0]);
        for (int i = 1; i < x.length; i++){
             s.append("+");
             s.append(newModel[0][i]);
             for (int j = i - 1; j >= 0; j--){
                 s.append("(x - ").append(x[j]).append(")");
             }
        }

        LabelSalida2.setText(s.toString());



    }//GEN-LAST:event_jButton1ActionPerformed

    private void jXHyperlink1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jXHyperlink1ActionPerformed
        parent.getPanelMain().cardChanger(PanelMain.INTERPOLACION);
    }//GEN-LAST:event_jXHyperlink1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel LabelSalida1;
    private javax.swing.JLabel LabelSalida2;
    private org.jdesktop.swingx.JXTable TablePuntos;
    private javax.swing.JTable TableResult;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private org.jdesktop.swingx.JXHyperlink jXHyperlink1;
    // End of variables declaration//GEN-END:variables

}
